<?php

/**
 * 
 * 删除城市
 * 
 */

include('./config/db.php');
$pgdb = new PGDB;

if(isset($_POST) && !empty($_POST['id'])){
    // 1. 获取市的ID
    $id = $_POST['id'];

    // 2.获取到市下所有的区ID
    $get_area_sql = "select * from public.management_t where par_id = $id";
    $area_res =@$pgdb->query($get_area_sql);
    while($area_row = (pg_fetch_row($area_res))){
        // 3. 开始删除区
        $area_del_sql = "delete from public.management_t where id = $area_row[0]";
        $res = @$pgdb->query($area_del_sql);
        if(!$res){
            die(json_encode(array('Code'=>0x000002,'Msg'=>'删除区失败')));
        }
    }
    // 4. 删除市
    $del_city_sql = "delete from public.management_t where id = $id";
    $res = @$pgdb->query($del_city_sql);
    if(!$res){
        die(json_encode(array('Code'=>0x000002,'Msg'=>'删除市失败')));
    }
    // 5. 返回结果
    die(json_encode(array('Code'=>0x000000,'Msg'=>'删除市成功')));
}else{
    die(json_encode(array('Code'=>0x000001,'Msg'=>'没有数据上传')));
}